package com.carkeeper.subject.service.impl;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.carkeeper.subject.dao.StoreMapper;
import com.carkeeper.subject.easy.WriteModel;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.service.EsayService;
import com.carkeeper.subject.service.StoreUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EsayServiceImpl implements EsayService {


    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private StoreUserService storeUserService;

    @Value("${esaypath}")
    private String esaypath;

    /**
     * 生成车主信息表格
     */
    @Override
    public void createEsay(Integer currentpage,Integer pagesize) {
        try {
            //文件输出位置
            OutputStream outputStream = new FileOutputStream(esaypath+currentpage+".xlsx");

            ExcelWriter writer = EasyExcelFactory.getWriter(outputStream);

            Sheet sheet = new Sheet(0,1,WriteModel.class);
            //传递第一个sheet名称
            sheet.setSheetName("服务商信息");

            writer.write(getWriteModels(currentpage,pagesize),sheet);
            writer.finish();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 填充信息到表格
     * @return
     */
    private List<WriteModel> getWriteModels(Integer currentpage,Integer pagesize) {
        PageHelper.startPage(currentpage, pagesize);

        HashMap<String, Object> map = new HashMap<>();
        map.put("page",currentpage.toString());
        map.put("size",pagesize.toString());
        List<Store> stores = storeUserService.findPage(map);

        PageInfo<Store> storePageInfo = new PageInfo<>(stores);

        List<Store> list = storePageInfo.getList();

        List<WriteModel> writeModels = new ArrayList<>();
        int i = 1;
        for (Store store : list) {
            WriteModel writeModel = new WriteModel();
            writeModel.setId(store.getId()+"");
            writeModel.setCode((store.getCode()));
            writeModel.setSimpleName(store.getSimpleName());
            writeModel.setAddress(store.getAddress());
            writeModel.setContactName(store.getContactName());
            writeModel.setContactPhone(store.getContactPhone());
            String status = store.getStatus();
            String isChain = store.getIsChain();
            if ("1".equals(isChain)) {
                status = "是";
            }
            if ("0".equals(isChain)) {
                status = "否";
            }

            if ("1".equals(status)) {
                status = "正常";
            }
            if ("3".equals(status)) {
                status = "锁定";
            }
            writeModel.setStatus(status);
            writeModel.setIsChain(isChain);
            writeModels.add(writeModel);

            i++;
        }
        return writeModels;

    }
}
